<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\ProductRepo;

use App\Repos\OrderPointRepo;

use App\Models\Cate;
use OSS;


class ProductController extends Controller
{
    public function index(Request $request)
    {
    	$where = [];
    	$data = $request->except(['token','_method','page']);
        $where = array_where($data, function ($value, $key) {
            return $where[$key] = $value;
        });
        //dd($where);
    	$list = ProductRepo::getListPages($where);
    	$cates = Cate::select(['id','name'])->get();
        $shopWhere['status'] = 1;
        
        
       
    	return view('admin.product.index',compact('list','where','cates'));
    }

    public function add(Request $request)
    {   
        $shopWhere['status'] = 1;
        
        $cates = Cate::select(['id','name'])->get(); 
          
        return view('admin.product.add',compact('cates')); 
    }

    public function save(Request $request)
    {

        $data = $request->all();
        try {
            $filecontent = file_get_contents($request->thumb->getRealPath());
            
            $filename = uniqid().'.'.$request->thumb->extension();
            OSS::putObject("xmistz", $filename, $filecontent);
            $data['thumb'] = 'http://'.env('OSS_BUCKET').'/'.$filename;
        } catch (\Exception $e) {
            dd($e->getMessage());
        }
       
      
        $rs['status'] = 'danger';
       
       
        $pics = [];
        if($request->pics){
            $pics = rtrim($request->pics,',');
            $pics = explode(',',$pics);
            
        }
        unset($data['pics']);
        $flag = ProductRepo::create($data,$pics);
        if($flag['status']==true) {
            $rs['status'] = 'success';
            $rs['msg']    = '操作成功';
            return redirect('zadmin/products')->with('rs',$rs);
        }
        $rs['msg'] = $flag['msg'];
        return back()->with('rs',$rs);
    }

    public function update(Request $request)
    {
        $rs['status'] = 'danger';
        $data = $request->except(['_token','id']);
        $id = $request->id;
        $pics = [];
        if($request->pics){
            $pics = rtrim($request->pics,',');
            $pics = explode(',',$pics);
            
        }
        unset($data['pics']);

        //dd($data);
        $flag = ProductRepo::update($id,$data,$pics);
        if($flag['status']==true) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return redirect('zadmin/products')->with('rs',$rs);
        } 

        $rs['msg'] = $flag['msg'];
        return redirect('zadmin/products')->with('rs',$rs);
    }

    public function edit(Request $request)
    {
        $cates = Cate::select(['id','name'])->get(); 
        $where['id'] = $request->id; 
        $data  = ProductRepo::find($where); 
       
       
           
       
        return view('admin.product.edit',compact('cates','data'));
    }

    public function setThumb(Request $request)
    {
        $rs['status'] = 'success';
        $rs['msg']    = '操作成功';
        $id    = $request->product_id;
        $thumb = $request->pic;

        $flag = ProductRepo::setThumb($id,$thumb);
       
        return redirect('zadmin/products')->with('rs',$rs);
       
    }

    

    

    /**
     * 修改推荐的状态
     */
    public function changeRec(Request $request)
    {
        $id             = $request->id;
        $data['is_rec'] = (int)$request->rec;        
        $flag = ProductRepo::ChangeStatus($id,$data);        
        return back();
    }

    

    public function destroy($id)
    {
        ProductRepo::destroy($id);
        return back();
    }


    public function makeZip(Request $request)
    {
        $where['product_id'] = $request->product_id;
        $product = ProductRepo::find(['id'=>$request->product_id]);
        $imgs    = ProductRepo::imgs($where);

        $filename = time();
        $zip_file = $product->code.'.zip';
        # create new zip object
        $zip =  new \ZipArchive();

        # create a temp file & open it
      
        $zip->open($zip_file, \ZipArchive::CREATE);

        # loop through each file
        foreach ($imgs as $val) {
            $file_url = env('OSS_URL').$val->imgurl;
            $download_file = file_get_contents($file_url);

            #add it to the zip
            $zip->addFromString(basename($file_url), $download_file);
        }

        # close zip
        $zip->close();


        $file_content = file_get_contents(public_path($zip_file));
        OSS::putObject("xmistz", $zip_file, $file_content);      
        unlink(public_path($zip_file));

        $product->zip = $zip_file;
        $product->save();
        return back();



       
    }

    
    

}
